﻿/*
Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/

//function getXMLDatasource() {
//    var hDataSource = document.getElementById("datasourse");
//    return hDataSource.value;
//}

CKEDITOR.dialog.add('textfield', function (editor) {

    var _caption = '';
    var _idName = '';
    //var SelecctDatasource = DIF.MasterData.getMasterFieldData();
    //var SelecctDatasource = [];



    var autoAttributes =
	{
	    value: 1,
	    size: 1,
	    maxLength: 1
	};

    var acceptedTypes =
	{
	    text: 1,
	    password: 1
	};

    return {
        title: editor.lang.textfield.title,
        minWidth: 350,
        minHeight: 200,
        onShow: function () {
            delete this.textField;
            //var selDatafields = this.definition.dialog.getContentElement('info', 'datafields');
            //            this._.contents.info.datafields.items = SelecctDatasource;

            //            this.showPage(this.id);





            var element = this.getParentEditor().getSelection().getSelectedElement();

            if (element && element.getName() == "input" &&
					(acceptedTypes[element.getAttribute('type')] || !element.getAttribute('type'))) {
                this.textField = element;
                this.setupContent(element);
            }
        },
        onOk: function () {
            var editor,
				element = this.textField,
				isInsertMode = !element;

            if (isInsertMode) {
                editor = this.getParentEditor();
                element = editor.document.createElement('input');
                element.setAttribute('type', 'text');
            }

            if (isInsertMode)
                editor.insertElement(element);
            this.commitContent({ element: element });
        },
        onLoad: function () {

            //                    var pID = editor.document.getSelection().getSelectedElement().getParent().getParent().getParent().getChildren().getItem(1).getId();
            //                    var selDatafields = this.definition.dialog.getContentElement('info', 'datafields');
            //                    if (pID == "data_row") {
            //                        DIF.DetailsData.init(_DetailsTableName);
            //                         //SelecctDatasource= DIF.DetailsData.getDetailsFieldData();
            //                         selDatafields.items = DIF.DetailsData.getDetailsFieldData();
            //                         selDatafields.getElement().show();
            //                    } 
            //                    else {
            //                        DIF.MasterData.init(_MasterTableName);
            //                        selDatafields.items = DIF.MasterData.getMasterFieldData();
            //                        selDatafields.getElement().show();
            //                        //SelecctDatasource= DIF.MasterData.getMasterFieldData();
            //                    }



            //      var selDatafields = this.definition..dialog.getContentElement('info', 'datafields');
            //            var ParentElement = element.getParent().getParent();
            //            if (ParentElement != null) {
            //                var pID = ParentElement.getAttribute('id');
            //                if (pID == "data_row") {
            //                    DIF.DetailsData.init(_DetailsTableName);
            //                    selDatafields.items = DIF.DetailsData.getDetailsFieldData();
            //                    selDatafields.getElement().show();

            //                }
            //                else {
            //                    DIF.MasterData.init(_MasterTableName);
            //                    selDatafields.items = DIF.MasterData.getMasterFieldData();
            //                    selDatafields.getElement().show();
            //                }
            //            }


            var autoSetup = function (element) {
                var value = element.hasAttribute(this.id) && element.getAttribute(this.id);
                this.setValue(value || '');
            };

            var autoCommit = function (data) {
                var element = data.element;
                var value = this.getValue();

                if (value)
                    element.setAttribute(this.id, value);
                else
                    element.removeAttribute(this.id);
            };

            this.foreach(function (contentObj) {
                if (autoAttributes[contentObj.id]) {
                    contentObj.setup = autoSetup;
                    contentObj.commit = autoCommit;
                }
            });
        },
        contents: [
			{

			    id: 'info',
			    label: editor.lang.textfield.title,
			    title: editor.lang.textfield.title,
			    elements: [
                   {
                       id: 'dataDetailsfields',
                       type: 'select',
                       style: 'width:100%;',
                       label: editor.lang.textfield.datafields,
                       'default': 'text',
                       accessKey: 'D',
                       items: SelecctDetailsDatasource,
                       onChange: function (obj, element) {

                           var isExist = $.inArray(this.getValue(), ControlArray);
                           if (isExist < 0) {
                               var dialog = this.getDialog();
                               var selectValue = this.getValue();

                               if (this.items.length > 0) {
                                   var Item = jlinq.from(this.items).equals('1', selectValue).select();
                                   if (typeof (Item) != "undefined") {
                                       if (typeof (Item[0]) != "undefined") { _caption = Item[0][0]; }
                                   } 
                               }
                               var textfieldName = dialog.getContentElement('info', '_cke_saved_name');
                               textfieldName.setValue(selectValue);
                               textfieldName.getElement().show();
                           } else {
                               if (obj.data.value != _idName) {
                                   alert(editor.lang.select.isexist_datafields);
                               }
                           }
                       },
                       setup: function (element) { 
                           var dialog = this.getDialog();
                           var pID = editor.document.getSelection().getSelectedElement().getParent().getParent().getParent().getChildren().getItem(1).getId();
                           var selDatafields = dialog.getContentElement('info', 'datafields');
                           var selDetailsDatafields = dialog.getContentElement('info', 'dataDetailsfields');
                           if (pID == "data_row") {
                               this.getElement().show()
                               selDatafields.getElement().hide();
                           }
                           else {
                               this.getElement().hide();
                               selDatafields.getElement().show();
                           }


                           _idName = element.getAttribute('id');
                           this.setValue(element.getAttribute('datafields'));
                       },
                       commit: function (data) {
                           var element = data.element;
                           if (this.getValue()) {
                               element.setAttribute('datafields', this.getValue());
                               element.setAttribute('title', _caption);
                           }
                           else {
                               element.removeAttribute('datafields');
                               element.removeAttribute('title');
                           }
                       }
                   },
                    {
                        id: 'datafields',
                        type: 'select',
                        style: 'width:100%;',
                        label: editor.lang.textfield.datafields,
                        'default': 'text',
                        accessKey: 'D',
                        items: SelecctMasterDatasource,
                        onChange: function (obj, element) {

                            var isExist = $.inArray(this.getValue(), ControlArray);
                            if (isExist < 0) {
                                var dialog = this.getDialog();
                                var selectValue = this.getValue();

                                if (this.items.length > 0) {
                                    var Item = jlinq.from(this.items).equals('1', selectValue).select();
                                    if (typeof (Item) != "undefined") {
                                        if (typeof (Item[0]) != "undefined") { _caption = Item[0][0]; }
                                    } 
                                }
                                var textfieldName = dialog.getContentElement('info', '_cke_saved_name');
                                textfieldName.setValue(selectValue);
                                textfieldName.getElement().show();
                            } else {
                                if (obj.data.value != _idName) {
                                    alert(editor.lang.select.isexist_datafields);
                                }
                            }
                        },
                        setup: function (element) {
                          

                            //var element = this.getParentEditor().getSelection().getSelectedElement();

                            //                            this.items = SelecctDatasource;
                            //                            this.getElement().show();

                            _idName = element.getAttribute('id');
                            this.setValue(element.getAttribute('datafields'));
                        },
                        commit: function (data) {
                            var element = data.element;
                            //                            element.setAttribute('datafields', this.getValue());
                            //                            element.setAttribute('title', _caption);


                            if (this.getValue()) {
                                element.setAttribute('datafields', this.getValue());
                                element.setAttribute('title', _caption);
                            }
                            else {
                                element.removeAttribute('datafields');
                                element.removeAttribute('title');
                            }
                        }
                    },
					{
					    type: 'hbox',
					    widths: ['50%', '50%'],
					    children:
						[
							{
							    id: '_cke_saved_name',
							    type: 'text',
							    label: editor.lang.textfield.name,
							    'default': '',
							    accessKey: 'N',
							    setup: function (element) {
							        this.setValue(
											element.data('cke-saved-name') ||
											element.getAttribute('name') ||
											'');
							    },
							    commit: function (data) {
							        var element = data.element;

							        if (this.getValue()) {
							            element.data('cke-saved-name', this.getValue());
							            element.setAttribute('id', this.getValue());
							            element.setAttribute('class', 'textInput large');
							        }
							        else {
							            element.data('cke-saved-name', false);
							            element.removeAttribute('name');
							            element.removeAttribute('id');
							            element.removeAttribute('class');
							        }
							    }
							},
							{
							    id: 'value',
							    type: 'text',
							    label: editor.lang.textfield.value,
							    'default': '',
							    accessKey: 'V'
							}
						]
					},
					{
					    type: 'hbox',
					    widths: ['50%', '50%'],
					    children:
						[
							{
							    id: 'size',
							    type: 'text',
							    label: editor.lang.textfield.charWidth,
							    'default': '',
							    accessKey: 'C',
							    style: 'width:50px',
							    validate: CKEDITOR.dialog.validate.integer(editor.lang.common.validateNumberFailed)
							},
							{
							    id: 'maxLength',
							    type: 'text',
							    label: editor.lang.textfield.maxChars,
							    'default': '',
							    accessKey: 'M',
							    style: 'width:50px',
							    validate: CKEDITOR.dialog.validate.integer(editor.lang.common.validateNumberFailed)
							}
						],
					    onLoad: function () {
					        // Repaint the style for IE7 (#6068)
					        if (CKEDITOR.env.ie7Compat)
					            this.getElement().setStyle('zoom', '100%');
					    }
					},
					{
					    id: 'type',
					    type: 'select',
					    label: editor.lang.textfield.type,
					    'default': 'text',
					    accessKey: 'M',
					    items:
						[
							[editor.lang.textfield.typeText, 'text'],
							[editor.lang.textfield.typePass, 'password']
						],
					    setup: function (element) {
					        this.setValue(element.getAttribute('type'));
					    },
					    commit: function (data) {
					        var element = data.element;

					        if (CKEDITOR.env.ie) {
					            var elementType = element.getAttribute('type');
					            var myType = this.getValue();

					            if (elementType != myType) {
					                var replace = CKEDITOR.dom.element.createFromHtml('<input type="' + myType + '"></input>', editor.document);
					                element.copyAttributes(replace, { type: 1 });
					                replace.replace(element);
					                editor.getSelection().selectElement(replace);
					                data.element = replace;
					            }
					        }
					        else
					            element.setAttribute('type', this.getValue());
					    }
					}
				]
			}
		]
    };
});
